package cn.buk.tms.flight.dto.response;

import cn.buk.common.flight.dto.FlightBasicInfo;
import cn.buk.tms.entity.order.flight.FlightRefundOrder;
import cn.buk.tms.entity.order.flight.FlightRefundOrderDetail;
import cn.buk.tms.common.dto.CustomerDto;
import cn.buk.tms.common.dto.PaymentMethodDto;
import cn.buk.tms.common.dto.SupplierDto;
import cn.buk.tms.common.dto.flight.BaseRefundOrderDto;
import cn.buk.tms.dto.AttachmentDto;
import cn.buk.tms.dto.SmsDetailDto;
import cn.buk.common.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class RefundOrderResponseDto extends BaseRefundOrderDto {

  /**
   * 外部退票单号(cz ndc)
   */
  private String externalOrderNo;

  private List<AttachmentDto> attachments;

  public static RefundOrderResponseDto createBy(FlightRefundOrder o) {
    RefundOrderResponseDto dto = new RefundOrderResponseDto();
    BeanUtils.copyProperties(o, dto, "flights");

    dto.setOrderStatus(o.getStatus());
    dto.setParValue(o.getParvalue());
    if (o.getCustomer() != null) {
      dto.setCustomer(CustomerDto.create(o.getCustomer().getId(), o.getCustomer().getVipName(), o.getCustomer().getCustomerCode()));
    } else {
      dto.setCustomer(CustomerDto.create(0, null, null));
    }

    for(FlightRefundOrderDetail d: o.getFlights()) {
      FlightBasicInfo flightInfoDto = new FlightBasicInfo();
      BeanUtils.copyProperties(d.getFlight(), flightInfoDto);

      try {
        if (StringUtils.isNotBlank(d.getFlight().getDdate())) {
          flightInfoDto.setDdate(DateUtil.convertToDate(d.getFlight().getDdate()));
        }
      } catch (ParseException e) {
        e.printStackTrace();
      }

      dto.getFlights().add(flightInfoDto);
    }

    if (o.getSupplier() != null) {
      dto.setSupplier(SupplierDto.create(o.getSupplier().getId(), o.getSupplier().getName()));
    }

    if (o.getPaymentMethod() != null) {
      dto.setPaymentMethod(PaymentMethodDto.create(o.getPaymentMethod().getId(), o.getPaymentMethod().getName()));
    }

    return dto;
  }

  /**
   * 用于输出到页面，很多代码是用status判断订单状态
   * @return
   */
  public int getStatus() {
    return this.getOrderStatus();
  }

  /**
   * 收客时此票的成本
   */
  private Double cost;

  /**
   * 航司退票费
   */
  private double airRefundCharge;

  /**
   * 预计航司实际退款金额（或同行退）
   */
  private Double airlineRefundAmount;

  /**
   * 航司实际退款金额
   */
  private Double airlineRefundAmountFinal;

  /**
   * 出票员：处理退票
   */
  private String ticketer;

  /**
   * 收款员（如果是月结订单，该值为空)
   */
  private String cashier;

  /**
   * 供应商
   */
  private SupplierDto supplier;

  /**
   * 支付方式
   */
  private PaymentMethodDto paymentMethod;

  /**
   * 相关的短信
   */
  private List<SmsDetailDto> smsDetails;

  public Double getCost() {
    return cost;
  }

  public void setCost(Double cost) {
    this.cost = cost;
  }

  public double getAirRefundCharge() {
    return airRefundCharge;
  }

  public void setAirRefundCharge(double airRefundCharge) {
    this.airRefundCharge = airRefundCharge;
  }

  public Double getAirlineRefundAmount() {
    return airlineRefundAmount;
  }

  public void setAirlineRefundAmount(Double airlineRefundAmount) {
    this.airlineRefundAmount = airlineRefundAmount;
  }

  public Double getAirlineRefundAmountFinal() {
    return airlineRefundAmountFinal;
  }

  public void setAirlineRefundAmountFinal(Double airlineRefundAmountFinal) {
    this.airlineRefundAmountFinal = airlineRefundAmountFinal;
  }

  public String getTicketer() {
    return ticketer;
  }

  public void setTicketer(String ticketer) {
    this.ticketer = ticketer;
  }

  public String getCashier() {
    return cashier;
  }

  public void setCashier(String cashier) {
    this.cashier = cashier;
  }

  public SupplierDto getSupplier() {
    return supplier;
  }

  public void setSupplier(SupplierDto supplier) {
    this.supplier = supplier;
  }

  public PaymentMethodDto getPaymentMethod() {
    return paymentMethod;
  }

  public void setPaymentMethod(PaymentMethodDto paymentMethod) {
    this.paymentMethod = paymentMethod;
  }

  public List<SmsDetailDto> getSmsDetails() {
    if (smsDetails == null) {
      smsDetails = new ArrayList<>();
    }
    return smsDetails;
  }

  public void setSmsDetails(List<SmsDetailDto> smsDetails) {
    this.smsDetails = smsDetails;
  }

  public List<AttachmentDto> getAttachments() {
    if (attachments == null) {
      attachments = new ArrayList<>();
    }
    return attachments;
  }

  public void setAttachments(List<AttachmentDto> attachments) {
    this.attachments = attachments;
  }

  public String getExternalOrderNo() {
    return externalOrderNo;
  }

  public void setExternalOrderNo(String externalOrderNo) {
    this.externalOrderNo = externalOrderNo;
  }
}
